package com.tutor.lzm.function.tutor;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.bean.SelectTime;
import com.tutor.lzm.bean.TimeType;
import com.tutor.lzm.http.response.TutorRequiresSearchBean;
import com.tutor.lzm.function.teacher.adapter.GridSelectTimeAdapter;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.ApplyForTeacherRequest;
import com.tutor.lzm.http.request.TutorDetailRequest;
import com.tutor.lzm.utils.ProjectStringUtils;
import com.tutor.syslib.Logs;
import com.tutor.syslib.util.MessageTools;
import com.tutor.syslib.view.MyGridView;
import com.tutor.syslib.view.RichTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zzgpc on 2017-10-5.
 * 任务详情
 */
public class TutorDetailAct extends BaseActivity {

    @BindView(R.id.img_subject)
    ImageView imgSubject;
    @BindView(R.id.tv_require)
    TextView tvRequire;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.tv_teach_price)
    TextView tvTeachPrice;
    @BindView(R.id.tv_teacher_type)
    TextView tvTeacherType;
    @BindView(R.id.tv_teacher_year)
    TextView tvTeacherYear;
    @BindView(R.id.tv_teacher_sex)
    TextView tvTeacherSex;
    @BindView(R.id.gridView_select_time)
    MyGridView myGridView;
    @BindView(R.id.rl_submit_resume)
    RelativeLayout rlSubmitResume;
    @BindView(R.id.tv_submit_resume)
    RichTextView tvSubmitResume;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    private String searchId;
    private GridSelectTimeAdapter adapter;

    public static void start(Context context, String searchId) {
        Intent intent = new Intent(context, TutorDetailAct.class);
        intent.putExtra("searchId", searchId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_tutor_detail);
        ButterKnife.bind(this);
        initTitle("任务详情");

        searchId = getIntent().getStringExtra("searchId");
        getTutorDetail();
    }


    private void refresh(TutorRequiresSearchBean bean) {

        Picasso.with(this).load(bean.categoryImageText).into(imgSubject);
        tvRequire.setText("需" + bean.gradeIdText + ProjectStringUtils.getEduForID(bean.categoryId) + "家教");
        tvArea.setText(bean.areaText);
        tvTeachPrice.setText(bean.price + "元/小时");
        tvTeacherType.setText(bean.teacherTypeText);
        tvTeacherYear.setText(bean.workingAge + "年");
        tvTeacherSex.setText(ProjectStringUtils.getSex(bean.teacherSex));
        tvDescription.setText(bean.description);
        if (bean.isPost == 1) {
            rlSubmitResume.setClickable(false);
            rlSubmitResume.setBackgroundDrawable(getResources().getDrawable(R.color.gray_audit));
            tvSubmitResume.setText("已投递简历");
        } else {
            rlSubmitResume.setClickable(true);
            tvSubmitResume.setText("投递简历");
            rlSubmitResume.setBackgroundDrawable(getResources().getDrawable(R.color.blue_audit));
        }
        adapter = new GridSelectTimeAdapter(this);
        adapter.setClick(false);
        myGridView.setAdapter(adapter);
        List<SelectTime> timeList = bean.timeList;
        for (int i = 0; i < timeList.size(); i++) {
            SelectTime selectTime = timeList.get(i);
            List<TimeType> timeTypeList = selectTime.timeTypeList;
            for (int j = 0; j < timeTypeList.size(); j++) {
                TimeType timeType = timeTypeList.get(j);
                int pos = selectTime.weekType + (timeType.timeType - 1) * 8;
                adapter.setSelectPosFlag(pos);
            }
        }
        adapter.notifyDataSetChanged();
    }

    TutorRequiresSearchBean bean;

    /**
     * 获取家教需求详情
     */
    private void getTutorDetail() {
        showDialog();
        final TutorDetailRequest request = new TutorDetailRequest();
        request.setSearchId(searchId);
        request.setTeacherId(AppSession.getInstance().getUserId());
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                bean = (TutorRequiresSearchBean) response;
                if (bean != null) {
                    refresh(bean);
                } else {
                    MessageTools.showToast(TutorDetailAct.this, "获取详情失败");
                    MessageTools.showDialogFinish(oThis, "加载详情失败...");
                }
                closeDialog();
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                closeDialog();
                Logs.d(tag, "getTutorDetail    code" + code + "      msg=" + msg);
                MessageTools.showDialogFinish(oThis, "加载详情失败...");
            }
        });

    }

    @OnClick(R.id.rl_submit_resume)
    public void onViewClicked() {

        ApplyForTeacherRequest request = new ApplyForTeacherRequest();
        request.teacherId = AppSession.getInstance().getUserId();
        request.userId = AppSession.getInstance().getUserId();
        request.searchId = bean.searchId;
        showMyDialog("正在投递");
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                MessageTools.showDialogOk(oThis, "申请递交成功");

                tvSubmitResume.setText("已投递简历");
                rlSubmitResume.setClickable(false);
                closeDialog();
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                MessageTools.showDialogOk(oThis, msg);
                closeDialog();
            }
        });
    }
}
